<template>
	<view class="login">
		<view class="home-top">
			<view class="form">
				<block v-if="istoggalLogin">
					<view class="inputWrapper">
						<image class="icon" src="../../../static/loginImg/user.png" mode=""></image>
						<input class="input" v-model="mtUser" type="text" placeholder="输入账号" required />
					</view>
					<view class="inputWrapper">
						<image class="icon" src="../../../static/loginImg/pwd.png" mode=""></image>
						<input class="input" v-model="mtPwd" type="password" placeholder="填写登录密码" required />
					</view>
				</block>
				<block v-if="!istoggalLogin">
					<view class="inputWrapper">
						<image class="icon" src="../../../static/loginImg/user.png" mode=""></image>
						<input class="input" v-model="userPhone" type="text" placeholder="输入手机号" required />
					</view>
					<view class="inputWrapper">
						<image class="icon" src="../../../static/loginImg/pwd.png" mode=""></image>
						<input class="input" v-model="captcha" type="text" placeholder="填写验证码" required />
						<button class="code" @click="code" :disabled="disabled">{{btnstr}}</button>
					</view>
				</block>
				<view class="toggalLogin" @click="toggalLogin">{{loginWay}}</view>
				<button v-if="istoggalLogin" class="btnValue" @click="btnLogin()">立即登录11</button>
				<button v-if="!istoggalLogin" class="btnValue" @tap="loginMobile()">立即登录22</button>
				<view class="agreement">
					<checkbox @click="changeBox" :checked="state" color="#00aa00" class="c-checkbox"></checkbox>
					<view style="font-size: 24rpx; margin-left: 20rpx;">我已阅读并同意
						<text style="color: red; font-size: 20rpx;">《住享家》《隐私协议》</text>
					</view>
				</view>
				<view class="wx-login">
					<view class="dsflogin">
						<view class="shortLine"></view>
						<text style="color: #333;">第三方登录</text>
						<view class="shortLine"></view>
					</view>
					<image style="width: 50rpx; height: 50rpx; display: block; "
						src="../../../static/loginImg/weChatLogo.png" mode="widthFix" @click="toWechat">
					</image>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		loginH5,
		registerVerify,
		getUserInfo,
		loginMobile
	} from "@/api/user";
	const BACK_URL = "login_back_url";
	export default {
		name: "Login",
		data() {
			return {
				istoggalLogin: false, //切换登录
				loginWay: "使用手机号登录",
				state: true,
				mtUser: "", //美团账号
				mtPwd: "", //美团密码
				userPhone: "", //手机号
				captcha: "", //验证码
				disabled: false, //是否禁用按钮
				btnstr: "获取验证码"
			}
		},
		methods: {
			// 是否点击协议框
			changeBox(e) {
				this.state = !this.state
				console.log(this.state)
			},
			//切换登录
			toggalLogin() {
				this.istoggalLogin = !this.istoggalLogin
				if (this.istoggalLogin) {
					this.loginWay = "使用手机号登录"
				} else {
					this.loginWay = "使用账号密码登录"
				}
			},
			//微信登录
			toWechat() {
				uni.navigateTo({
					url: '/pages/users/login/weChatLogin'
				})
			},
			//手机验证码登录
			async loginMobile() {
				if (this.state == false) {
					uni.showToast({
						title: "请勾选协议",
						icon: "none"
					})
					return false
				}
				let that = this;
				if (!that.userPhone) return that.$util.Tips({
					title: '请填写手机号码'
				});
				if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(that.userPhone)) return that.$util.Tips({
					title: '请输入正确的手机号码'
				});
				if (!that.captcha) return that.$util.Tips({
					title: '请填写验证码'
				});
				if (!/^[\w\d]+$/i.test(that.captcha)) return that.$util.Tips({
					title: '请输入正确的验证码'
				});
				loginMobile({
						phone: that.userPhone,
						captcha: that.captcha,
						spread_spid: that.$Cache.get("spread")
					})
					.then(res => {
						let data = res.data;
						let newTime = Math.round(new Date() / 1000);
						this.$store.commit("LOGIN", {
							'token': res.data.token
						});
						that.getUserInfo(data);
					})
					.catch(res => {
						that.$util.Tips({
							title: res
						});
					});
			},
			// 获取验证码
			async code() {
				if (this.state == false) {
					uni.showToast({
						title: "请勾选协议",
						icon: "none"
					})
					return false
				}
				let that = this;
				if (!that.userPhone) return that.$util.Tips({
					title: '请填写手机号码'
				});
				if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(that.userPhone)) return that.$util.Tips({
					title: '请输入正确的手机号码'
				});
				await registerVerify(that.userPhone)
					.then(res => {
						console.log("验证码：", res)
						that.$util.Tips({
							title: res.message
						});
						this.timewait(60);
					})
					.catch(err => {
						return that.$util.Tips({
							title: err
						});
					});
			},
			//倒计时
			timewait(t) {
				console.log("时间")
				setTimeout(() => {
					if (t >= 0) {
						this.btnstr = t + '重新获取验证码';
						t--;
						this.timewait(t);
						this.disabled = true
					} else {
						this.btnstr = '获取验证码';
						t = 60;
						this.disabled = false
					}
				}, 1000)
			},
			// 美团账户登录
			btnLogin() {
				if (this.state == false) {
					uni.showToast({
						title: "请勾选协议",
						icon: "none"
					})
					return false
				}
				let that = this;
				if (!that.mtUser) return that.$util.Tips({
					title: '请填写账号'
				});
				if (!/^[\w\d]{5,16}$/i.test(that.mtUser)) return that.$util.Tips({
					title: '请输入正确的账号'
				});
				if (!that.mtPwd) return that.$util.Tips({
					title: '请填写密码'
				});
				let params = {
					account: that.mtUser,
					password: that.mtPwd,
					spread_spid: 0
				}
				loginH5(params).then(async ({
					data
				}) => {
					this.$store.commit("LOGIN", {
						'token': data.token
					});
					await that.getUserInfo(data);
				}).catch(e => {
					that.$util.Tips({
						title: e
					});
				});
			},
			getUserInfo(data) {
				this.$store.commit("SETUID", data.uid);
				getUserInfo().then(res => {
					this.$store.commit("UPDATE_USERINFO", res.data);
					let backUrl = this.$Cache.get(BACK_URL) || "/pages/index/index";
					if (backUrl.indexOf('/pages/users/login/index') !== -1) {
						backUrl = '/pages/index/index';
					}
					if (!backUrl.startsWith('/')) {
						backUrl = `/${backUrl}`
					}
					uni.switchTab({
						url: backUrl,
						fail: () => {
							console.log('fail==++++++');
							uni.reLaunch({
								url: backUrl,
								fail: () => {
									console.log('fail');
								}
							});
						}
					});
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.login {
		min-height: 100vh;

		.home-top {
			position: relative;
			width: 140%;
			height: 470rpx;
			left: -120rpx;
			background-image: url(@/static/loginImg/bigLogo.png);
			background-color: #ffe2bc;
			background-repeat: no-repeat;
			background-size: cover;
			background-position: 0rpx -80rpx;
			border-radius: 0 0 50% 50%;

			/* #ifdef H5 */
			.form {
				position: absolute;
				display: flex;
				width: 500rpx;
				height: 700rpx;
				left: 50%;
				flex-direction: column;
				margin-top: 550rpx;
			}

			/* #endif */

			.form {
				position: absolute;
				display: flex;
				width: 500rpx;
				height: 700rpx;
				left: 25%;
				flex-direction: column;
				margin-top: 550rpx;

				.inputWrapper {
					display: flex;
					align-items: center;
					width: 500rpx;
					height: 100rpx;
					padding-left: 20rpx;
					border-radius: 40rpx;
					box-shadow: 1rpx 2rpx 8rpx 4rpx rgba(0, 0, 0, 0.3);
					margin-bottom: 20rpx;

					.icon {
						margin-right: 10rpx;
						width: 40rpx;
						height: 40rpx;
						display: block;
					}

					.code {
						height: 50rpx;
						width: 300rpx;
						background-color: #ffe2bc;
						font-size: 26rpx;
						line-height: 50rpx;
						color: #f5f5f5;
						margin-right: 20rpx;
						border-radius: 20rpx;
					}
				}

				.toggalLogin {
					font-size: 26rpx;
				}

				.btnValue {
					width: 500rpx;
					height: 100rpx;
					border-radius: 40rpx;
					background-color: #ffe2bc;
					text-align: center;
					color: white;
					box-shadow: 1rpx 2rpx 8rpx 4rpx rgba(0, 0, 0, 0.3);
					margin-top: 20rpx;
					line-height: 85rpx;
					margin-top: 40rpx;
					margin-bottom: 20rpx;
				}

				.agreement {
					display: flex;
					align-items: center;
					justify-content: start;
					margin-right: 10rpx;
				}

				.wx-login {
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
					margin-top: 150rpx;

					.dsflogin {
						width: 100%;
						height: 60rpx;
						display: flex;
						justify-content: space-between;
						align-items: center;

						.shortLine {
							width: 35%;
							height: 1rpx;
							background-color: #f5f5f5;
							box-shadow: 1rpx 2rpx 2rpx 1rpx rgba(0, 0, 0, 0.1);
						}
					}
				}
			}
		}
	}
</style>